using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Constants;

public partial class forums_controls_login_loginpanel : System.Web.UI.UserControl
{
    public event EventHandler UserLoggedIn;
    public event EventHandler UserLoggedOut;
    
    public UserViewMode Mode
    {
        get
        {
            if (mviewLogin.GetActiveView() == viewAuthenticated)
            {
                return UserViewMode.Authenticated;
            }
            else
            {
                return UserViewMode.Anonymous;
            }
        }
        set
        {
            switch (value)
            {
                case UserViewMode.Authenticated:
                    mviewLogin.SetActiveView(viewAuthenticated);
                    break;
                default:
                    mviewLogin.SetActiveView(viewAnonymous);
                    break;
            }
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (Managers.UserLoginManager.CurrentUser == null)
        {
            Mode = UserViewMode.Anonymous;
        }
        else
        {
            lblUsername.Text = Helpers.StringHelper.SafeFormat(Managers.UserLoginManager.CurrentUser.Username);
            Mode = UserViewMode.Authenticated;
        }
    }

    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        Managers.UserLoginManager.Logout();
        if (UserLoggedOut != null)
        {
            UserLoggedOut(this, new EventArgs());
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        DAL.User currentUser = Managers.UserLoginManager.Login(txtUsername.Text, txtPassword.Text);
        bool validLogin = (currentUser != null);
        pnlInvalidLogin.Visible = !validLogin;

        if (validLogin)
        {
            if (UserLoggedIn != null)
            {
                UserLoggedIn(this, new EventArgs());
            }
        }
    }
}
